package com.zzxz.practicaltrainingsys.config;

import com.zzxz.practicaltrainingsys.service.ChatRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class ScheduleConfig {

    @Autowired
    private ChatRecordService chatRecordService;

    /**
     * 每天凌晨2点清理30天前的聊天记录
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanOldChatRecords() {
        try {
            int deletedCount = chatRecordService.cleanOldChatRecords(30);
            System.out.println("定时清理旧聊天记录完成，共清理 " + deletedCount + " 条记录");
        } catch (Exception e) {
            System.err.println("定时清理聊天记录失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}